package utils;

import java.sql.*;

public class JDBCUtils {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {

//        Class.forName("com.mysql.jdbc.Driver");
         Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/chat?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String user = "root";
        String password = "191668";


        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;
    }
    public static void release(ResultSet rs, Statement stmt, Connection conn) {
            //关rs
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                rs = null;
            }
            release(stmt,conn);


    }
    public static void release(PreparedStatement pstmt, Connection conn) throws SQLException {
        if (pstmt != null) {
            pstmt.close();
        }
        pstmt = null;
        if (conn != null) {
            conn.close();
        }
        conn = null;
    }
    public static void release(ResultSet rs,PreparedStatement pstmt,Connection conn) throws SQLException {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            rs = null;
        }
        release(pstmt,conn);
    }
    public static void release(Statement stmt,Connection conn){
        if (stmt !=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn !=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            conn = null;
        }

    }
}



